package manager;

import javax.ejb.EJB;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import entidade.NivelAcesso;

import session.PessoaSession;

@Named
@SessionScoped
public class LoginManager {

	@EJB
	private PessoaSession pSession;
	private String login;

	public LoginManager() {
		super();
		try {
			init();
		} catch (Exception e) {
			
		}
		
	}
	
	private void init(){
		SecurityContext context = SecurityContextHolder.getContext();
		if (context instanceof SecurityContext) {
			Authentication authentication = context.getAuthentication();
			if (authentication instanceof Authentication) {
				login = ((User) authentication.getPrincipal()).getUsername();
			}
		}
	}

	public String teste() {
		String nivel = "ROLE_PUBLIC";
		try {
			nivel = pSession.buscarPorLogin(login).getNivelDeAutorizacao().getAutorizacao();
		} catch (Exception e) {
			
		}
		
		if (nivel.equals("ROLE_ADMIN")) {
			return "paginas/admin/gerenciamentoTurmas.jsf";
		}
		if (nivel.equals("ROLE_PROF")) {
			return "paginas/prof/viewTurma.jsf";
		}
		if (nivel.equals("ROLE_ATEND")) {
			return "paginas/atend/gerenciamentoAluno.jsf";
		}
		if (nivel.equals("ROLE_ALUN")) {
			return "paginas/aluno/viewAluno.jsf";
		}
		return "login.jsf";
	}
}